package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.TradeRecord;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import xtom.frame.util.XtomTimeUtil;

/**
 * 现金的交易记录
 * @author GuiyouZhang
 */
public class TradeAdapter extends BaseAdapter {

    private ArrayList<TradeRecord> records;

    public TradeAdapter(Context mContext, ArrayList<TradeRecord> records) {
        super(mContext);
        this.records = records;
    }

    @Override
    public int getCount() {
        int size = records == null ? 0 : records.size();
        return size == 0 ? 1 : records.size();
    }

    @Override
    public boolean isEmpty() {
        int size = records == null ? 0 : records.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_cash_trade, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        }
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_mark)
        TextView tvMark;
        @Bind(R.id.tv_quota)
        TextView tvQuota;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.tv_date)
        TextView tvDate;
        int position = -1;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setDatas(int position) {
            TradeRecord record = records.get(position);
//            if (record.getOtherpay_type().equals("0")) {//非代付
                // 消费类型:1消费；2赚取；4退款所得；5分成；6充值；7提现
                String keytype = record.getKeytype();
                if (keytype.equals("1")) {
                    tvMark.setText("-");
                    tvContent.setText("订单号：" + record.getBill_sn());
                    if (record.getOtherpay_type().equals("0")) {//非代付
                        tvName.setText("消费");
                    }else {
                        tvName.setText("代付：" + record.getUsername());
                    }
                } else if (keytype.equals("2")) {
                    tvName.setText("赚取所得");
                    tvMark.setText("+");
                    tvContent.setText("订单号：" + record.getBill_sn());
                } else if (keytype.equals("4")) {
                    tvName.setText("退款");
                    tvMark.setText("+");
                    tvContent.setText("订单号：" + record.getBill_sn());
                } else if (keytype.equals("5")) {
                    tvName.setText("分成：" + record.getUsername());
                    tvMark.setText("+");
                    tvContent.setText("订单号：" + record.getBill_sn());
                } else if (keytype.equals("6")) {
                    tvName.setText("充值");
                    tvMark.setText("+");
                } else if (keytype.equals("7")) {
                    tvName.setText("提现");
                    tvMark.setText("-");
                } else {
                    tvName.setText("未知类型");
                }
//            }else {//代付订单
//                tvMark.setText("-");
//                tvContent.setText("订单号：" + record.getBill_sn());
//                tvName.setText("代付："+record.getUsername());
//            }
            tvQuota.setText("¥" + uuUtils.formatAfterDot2(record.getTradefee()));
            tvDate.setText(XtomTimeUtil.TransTime(record.getRegdate(), "yyyy-MM-dd"));
        }
    }
}
